package TCPMain;

import static TCPMain.Constant.ACTION_PublishTask;
import static TCPMain.Constant.ACTION_SendFolder;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class FloderService extends Service {
    JSONObject Task=new JSONObject();
    private static final String TAG = "FloderService";
    private final FloderService.MyBinder binder = new FloderService.MyBinder();

    private List<Intent> intentList = new ArrayList<>();
    private int num=0;
    public FloderService() {
    }
    // 定义一个内部类，继承自 Binder
    public class MyBinder extends Binder {
        // 允许客户端调用 Service 的方法
        public FloderService getService() {
            return FloderService.this;
        }
    }
    // 广播接收器
    private final BroadcastReceiver taskReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //这里不能放阻塞操作，否则会无响应，因为这是广播
            try{
                if (ACTION_PublishTask.equals(intent.getAction())) {
                    Log.d(TAG, "onReceive: PublishTask");
                    // 解析广播数据
                    String task = intent.getStringExtra("Task");
                    Log.d(TAG, "onReceive: task: " + task);
                    Task=new JSONObject(task);
                    // 是否需要处理这个广播
                    if(Task.getString("Command").equals("SendFolder")){
                        Log.d(TAG, "onReceive: SendFolder");
                        //处理文件夹请求
                        new Thread(()-> {
                            try{
                            Task=new JSONObject(task);
                            if(Task.getString("Command").equals("SendFolder")){
                                Log.d(TAG, "onStartCommand: SendFolder");
                                Client FolderClient = new Client();
                                JSONObject response = FolderClient.SendFolder(Task);
                                Log.d(TAG, "onendCommand: " + response);
                                AllData.setAllJsonData(response);
                                // 在子线程内发送广播
                                Intent broadcastIntent = new Intent(ACTION_SendFolder);
                                broadcastIntent.putExtra("FolderData", response.toString());
                                LocalBroadcastManager.getInstance(FloderService.this)
                                        .sendBroadcast(broadcastIntent);
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }

                        }).start();
                        }else{
                        //丢弃广播
                        return;
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    };
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return binder;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        // 注册广播接收器
        IntentFilter filter = new IntentFilter("PublishTask");
        registerReceiver(taskReceiver, filter);
        Log.d(TAG, "onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     *
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "Service onDestroy");
    }

}